<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
use xiaoniuyun\SendSms;
class Login extends Base{
	public function sendSms()
	{
		if(!empty(input("param.phone"))){
			$phone=input("param.phone");
		}else{
			return json(['code' => 501, 'msg' => '请填写手机号']);
		}
		$verifyCode = rand(1000,9999);
		$result = Db::name('verify_code')->insertGetId(['phone' => $phone, 'code' => $verifyCode,'time' => time()]);
		if(!$result){
			return json(['code' => 502, 'msg' => '发送失败，稍后重试哟']);
		}else{
			$sendSms = new SendSms();
			$verifyCodearr=array("code" => $verifyCode);
			$result_content = $sendSms->sendSms($phone,$verifyCodearr,503);
			if($result_content->Message=='OK'){
				return json(['code' => 200, 'msg' => '发送成功','data'=>$verifyCode]);
			}else{
				return json(['code' => 503, 'msg' => '发送失败,错误码：'.$result_content->result.',错误说明：'.$result_content->Message]);
			}
		}
	}
	// 登录
	public function login()
	{
		if(input("param.identity_type")==1){//手机号短信验证码登录(注册)
			if(!empty(input("param.phone"))){
				$data['phone']=input("param.phone");
				if(!empty(input("param.verify_code"))){
					$code=input("param.verify_code");
				}else{
					return json(['code' => 503, 'msg' => '验证码不能为空']);exit();
				}
				$user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
				if ($user) {
					$verify_code=Db::name('verify_code')->where(array('phone'=>$data['phone']))->order('id desc')->value('code');
					if ($verify_code==$code) {
						$res['Token']=UserCrypt::do_mencrypt($user['id']);
						return json(['code' => 200, 'msg' => '登录成功','data'=>$res]);
					}else{
						return json(['code' => 504, 'msg' => '验证码错误']);
					}
				}else{
					$verify_code=Db::name('verify_code')->where(array('phone'=>$data['phone']))->order('id desc')->value('code');
					if ($verify_code==$code) {
						$data['time']=time();
						$data['uptime']=time();
						$id=Db::name('users')->insertGetId($data);
						if($id){
							$res['Token']=UserCrypt::do_mencrypt($id);
							return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
						}else{
							return json(['code' => 500, 'msg' => '失败,请重试']);
						}
					}else{
						return json(['code' => 505, 'msg' => '验证码错误']);
					}
					
				}
			}else{
				return json(['code' => 502, 'msg' => '手机号不能为空']);
			}
		}elseif(input("param.identity_type")==2){//微信登录
			if(!empty(input("param.openid"))){
				$openid=input("param.openid");
				$users_auth=Db::name('users_auth')->where(array('identity_type'=>input("param.identity_type"),'identifier'=>input("param.openid")))->find();
				if ($users_auth) {
					$res['Token']=UserCrypt::do_mencrypt($users_auth['user_id']);
					return json(['code' => 200, 'msg' => '登录成功','data'=>$res]);
				}else{
					if(!empty(input("param.phone"))){
						$phone=input("param.phone");
						if(!empty(input("param.verify_code"))){
							$code=input("param.verify_code");
							$verify_code=Db::name('verify_code')->where(array('phone'=>$data['phone']))->order('id desc')->value('code');
							if ($verify_code==$code) {
								$users=Db::name('users')->where(array('phone'=>$phone))->find();
								if ($users) {
									$data_auth['user_id']=$users['id'];
									$data_auth['identity_type']=input("param.identity_type");
									$data_auth['identifier']=$openid;
									$data_auth['time']=time();
									$auth_id=Db::name('users_auth')->insertGetId($data_auth);
									if ($auth_id) {
										$res['Token']=UserCrypt::do_mencrypt($users['id']);
										return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
									}else{
										return json(['code' => 500, 'msg' => '失败,请重试']);
									}
								}else{
									Db::startTrans();
            						try {
            							$data_user['phone']=$phone;
            							$data_user['time']=time();
										$data_user['uptime']=time();
										$uid=Db::name('users')->insertGetId($data_user);
										$data_auth['user_id']=$uid;
										$data_auth['identity_type']=input("param.identity_type");
										$data_auth['identifier']=$openid;
										$data_auth['time']=time();
										$auth_id=Db::name('users_auth')->insertGetId($data_auth);
						                Db::commit();
						                if($auth_id){
											$res['Token']=UserCrypt::do_mencrypt($uid);
											return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
						                }else{
						                    return json(['code' => 500, 'msg' => '提交事物失败！']);
						                }
						            } catch (\Exception $e) {
						                // 回滚事务
						                Db::rollback();
						                return json(['code' => 500, 'msg' => '事物回滚失败！']);
						            }
								}
							}else{
								return json(['code' => 505, 'msg' => '验证码错误']);
							}
						}else{
							return json(['code' => 504, 'msg' => '验证码不能为空']);exit();
						}
					}else{
						return json(['code' => 503, 'msg' => '手机号不能为空']);
					}
				}
			}else{
				return json(['code' => 502, 'msg' => 'openid呢']);
			}
		}elseif(input("param.identity_type")==3){//QQ登录
			if(!empty(input("param.openid"))){
				$openid=input("param.openid");
				$users_auth=Db::name('users_auth')->where(array('identity_type'=>input("param.identity_type"),'identifier'=>input("param.openid")))->find();
				if ($users_auth) {
					$res['Token']=UserCrypt::do_mencrypt($users_auth['user_id']);
					return json(['code' => 200, 'msg' => '登录成功','data'=>$res]);
				}else{
					if(!empty(input("param.phone"))){
						$phone=input("param.phone");
						if(!empty(input("param.verify_code"))){
							$code=input("param.verify_code");
							$verify_code=Db::name('verify_code')->where(array('phone'=>$data['phone']))->order('id desc')->value('code');
							if ($verify_code==$code) {
								$users=Db::name('users')->where(array('phone'=>$phone))->find();
								if ($users) {
									$data_auth['user_id']=$users['id'];
									$data_auth['identity_type']=input("param.identity_type");
									$data_auth['identifier']=$openid;
									$data_auth['time']=time();
									$auth_id=Db::name('users_auth')->insertGetId($data_auth);
									if ($auth_id) {
										$res['Token']=UserCrypt::do_mencrypt($users['id']);
										return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
									}else{
										return json(['code' => 500, 'msg' => '失败,请重试']);
									}
								}else{
									Db::startTrans();
            						try {
            							$data_user['phone']=$phone;
            							$data_user['time']=time();
										$data_user['uptime']=time();
										$uid=Db::name('users')->insertGetId($data_user);
										$data_auth['user_id']=$uid;
										$data_auth['identity_type']=input("param.identity_type");
										$data_auth['identifier']=$openid;
										$data_auth['time']=time();
										$auth_id=Db::name('users_auth')->insertGetId($data_auth);
						                Db::commit();
						                if($auth_id){
											$res['Token']=UserCrypt::do_mencrypt($uid);
											return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
						                }else{
						                    return json(['code' => 500, 'msg' => '提交事物失败！']);
						                }
						            } catch (\Exception $e) {
						                // 回滚事务
						                Db::rollback();
						                return json(['code' => 500, 'msg' => '事物回滚失败！']);
						            }
								}
							}else{
								return json(['code' => 505, 'msg' => '验证码错误']);
							}
						}else{
							return json(['code' => 504, 'msg' => '验证码不能为空']);exit();
						}
					}else{
						return json(['code' => 503, 'msg' => '手机号不能为空']);
					}
				}
			}else{
				return json(['code' => 502, 'msg' => 'openid呢']);
			}
		}else{
			return json(['code' => 501, 'msg' => '登录方式呢']);
		}
	}
}